package com.yl.ydk.presenter.impl

import android.content.Context
import androidx.fragment.app.Fragment
import com.yl.ydk.R
import com.yl.ydk.api.Api
import com.yl.ydk.base.BaseFragmentPageAdapter
import com.yl.ydk.base.BasePresenter
import com.yl.ydk.bean.DiseaseDetailBean
import com.yl.ydk.presenter.contract.DiseaseDetailActivityContract
import com.yl.ydk.ui.fragment.DiseaseDetailItemFragment
import rx.Observer
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import java.util.ArrayList

class DiseaseDetailActivityImpl : BasePresenter<DiseaseDetailActivityContract.View>(),
    DiseaseDetailActivityContract.Presenter<DiseaseDetailActivityContract.View> {

    private var mAdapter: BaseFragmentPageAdapter? = null
    private val mTitleList = ArrayList<String>()
    private val mFragments = ArrayList<Fragment>()

    override fun getGetDiseaseDetail(context: Context, vararg query: String?) {
        showLoadingDialog(context)
        addSubscrebe(
            Api.getInstance()?.getGetDiseaseDetail(*query)
                ?.observeOn(AndroidSchedulers.mainThread())
                ?.subscribeOn(Schedulers.io())
                ?.unsubscribeOn(Schedulers.io())
                ?.subscribe(object : Observer<DiseaseDetailBean?> {
                    override fun onCompleted() {}
                    override fun onError(e: Throwable) {
                        dismissLoadingDialog()
                        mView?.showError(e.toString())
                    }

                    override fun onNext(t: DiseaseDetailBean?) {
                        dismissLoadingDialog()
                        if (t?.data != null && t.code == 200) {

                            mTitleList.add("概述")
                            mTitleList.add("症状")
                            mTitleList.add("病因")
                            mTitleList.add("就医")
                            mTitleList.add("治疗")

                            mFragments.add(DiseaseDetailItemFragment.getInstance(t.data.summary))
                            mFragments.add(DiseaseDetailItemFragment.getInstance(t.data.symptom))
                            mFragments.add(DiseaseDetailItemFragment.getInstance(t.data.cause))
                            mFragments.add(DiseaseDetailItemFragment.getInstance(t.data.doctor))
                            mFragments.add(DiseaseDetailItemFragment.getInstance(t.data.cure))

                            t.data.name.let { mView?.getNameTextView()?.text = it }
                            val depts = t.data.department.replace(",", " ")
                            mView?.getDeptTextView()?.text = depts
                            mView?.getActivity()?.let {
                                mAdapter = BaseFragmentPageAdapter(
                                    it.supportFragmentManager,
                                    mFragments,
                                    mTitleList
                                )
                            }
                            mView?.getViewPager()?.adapter = mAdapter
                            mView?.getSlidingTabLayout()?.setViewPager(mView?.getViewPager())
                            mView?.getNestedScrollView()?.isFillViewport = true

                        } else {
                            mView?.showError(context.getString(R.string.connectFailure))
                        }
                    }
                })
        )
    }


}